package cn.ecsd.service.impl;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

import cn.ecsd.common.CommonDao;
import cn.ecsd.entity.BizClaimVoucher;
import cn.ecsd.entity.BizLeave;
import cn.ecsd.entity.SysEmployee;
import cn.ecsd.service.ClaimService;
import cn.ecsd.util.PageSupport;

/**
 * 报销单的业务实现类
 * 
 * @author 寇超强
 *
 */
public class ClaimServiceImpl implements ClaimService {

	private CommonDao commonDao;

	public CommonDao getCommonDao() {
		return commonDao;
	}

	public void setCommonDao(CommonDao commonDao) {
		this.commonDao = commonDao;
	}

	StringBuffer sb = new StringBuffer("");

	/**
	 * 分页查询报销单列表
	 */
	@Override
	public PageSupport getClaimPage(PageSupport page, BizClaimVoucher voucher) {
		sb.setLength(0);
		if (voucher != null) {
			if (voucher.getStatus() != null && !"全部".equals(voucher.getStatus())) {
				sb.append(" and c.status='" + voucher.getStatus() + "'");
			}
			if (voucher.getStartDate() != null) {
				sb.append(" and c.createTime> '" + voucher.getStartDate() + "'");
			}
			if (voucher.getEndDate() != null) {
				sb.append(" and c.createTime< '" + voucher.getEvent() + "'");
			}
		}
		Map<String, Object> session = ActionContext.getContext().getSession();
		if ("部门经理".contentEquals((String) (session.get("employee_position")))) {
			// 如果是部门经理登陆，查询自己部门的员工的报销单（除了新创建），按照状态升序排列
			sb.append(" and c.sysEmployeeByCreateSn.sysDepartment.id= "
					+ ((SysEmployee) session.get("employee")).getSysDepartment().getId() + " and c.status !='新创建'"
					+ "  order by c.status desc");
		} else if ("员工".contentEquals((String) (session.get("employee_position")))) {
			// 如果是员工登陆，查询自己的所有报销单，按照修改时间降序排列
			sb.append(" and c.sysEmployeeByCreateSn.sn= '" + ((SysEmployee) session.get("employee")).getSn()
					+ "' order by c.modifyTime desc");
		} else if ("财务".contentEquals((String) (session.get("employee_position")))) {
			// 如果是财务登陆，查看所有人的报销单，按照状态升序排列（除了已打回）
			sb.append(" and c.status!='已打回' and c.status!='新创建' order by c.status ");
		} else {
			// 总经理登陆要查看所有人的信息 不包括新创建的
			sb.append(" and c.status !='新创建' order by c.status ");
		}

		String sql = "from BizClaimVoucher c where 1=1 " + sb.toString();
		List<BizClaimVoucher> list = commonDao.queryPage(sql, page);
		page.setTotalCount(getTotalCount());
		page.setItems(list);
		return page;
	}

	/**
	 * 查询总共条数
	 */
	@Override
	public int getTotalCount() {
		String sql = "select count(*) from BizClaimVoucher as c where 1=1 " + sb.toString();
		return commonDao.getCount(sql);
	}

	/**
	 * 根据id获得报销单一个对象
	 */
	@Override
	public BizClaimVoucher getClaimVoucherById(int id) {
		// TODO Auto-generated method stub
		return (BizClaimVoucher) commonDao.get(BizClaimVoucher.class, id);
	}

	/**
	 * 修改报销单信息
	 */
	@Override
	public void updateClaimVoucher(BizClaimVoucher voucher) {
		// TODO Auto-generated method stub
		commonDao.update(voucher);
	}

	/**
	 * 根据登陆的员工查询上级对象
	 */
	@Override
	public SysEmployee getManagerByDeparId(SysEmployee e, double total) {
		String sql = "from SysEmployee e where e.sysDepartment.id=" + e.getSysDepartment().getId();
		if (e.getSysPosition().getId() == 1) {
			// 如果职位是员工，查询自己的部门经理
			sql += " and e.sysPosition.id=2";
		} else if (e.getSysPosition().getId() == 2 && total > 5000) {
			// 职位是经理，并且金额大于5000，查看自己的的总经理
			sql = " from SysEmployee e where e.sysPosition.id=3";
		} else {
			// 职位是总经理，直接提交财务
			sql += "from SysEmployee e where e.sysPosition.id=4";
		}

		if (commonDao.search(sql).size() != 0) {
			return (SysEmployee) commonDao.search(sql).get(0);
		}
		return null;
	}
	/**
	 * 查询一个财务
	 */
	@Override
	public SysEmployee getFinance() {
		String sql="from SysEmployee e where e.sysPosition.id=4";
		if (commonDao.search(sql).size() != 0) {
			return (SysEmployee) commonDao.search(sql).get(0);
		}
		return null;
	}
	/**
	 * 新增报销单
	 */
	@Override
	public void addClaimVoucher(BizClaimVoucher voucher) {
		// TODO Auto-generated method stub
		commonDao.add(voucher);
	}

	@Override
	public void deleteClaimVoucherById(BizClaimVoucher voucher) {
		// TODO Auto-generated method stub
		commonDao.del(voucher);
	}
	
}
